home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
3D GFX
/
3D GFX.iso
/
amiutils
/
e_h
/
gfx2grob
/
src
/
iffstuff.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-30
|
6KB
|
210 lines
#ifdef AMIGA
/**************************************************************************
Amiga iff.library functions
Copyright (C) 1994 by Alexandros Loghis, All Rights Reserved
**************************************************************************/
#include <stdio.h> /* remove, fopen, fclose, fread, fwrite */
#include <stdlib.h> /* malloc, free */
#include <proto/exec.h>
#include <proto/dos.h>
#include <graphics/gfx.h>
#include "gfx2grob.h"
#include "PrintMsg.h"
#include "Convert.h"
#include "iff.h"
#include "IffStuff.h"
#define CLRPRMSGBI(A) { ClearBI(FLock, IFFBase); \
PrintMsg A ; \
}
#define CLRPRMSGIB(M) { ClearIB(IffFile, IFFBase); \
PrintMsg(M, gv->Options.InputfileName); \
}
static int IffVersion = IFFVERSION; /* to get address of int */
char TEMPFILENAME[] = "RAM:gfx2grob.tmp";
static void ClearBI(BPTR FLock, struct Library *IFFBase);
static MsgTypePtr GetErr(int IffErr);
static void ClearIB(IFFL_HANDLE IffFile, struct Library *IFFBase);
/*************************************************************************/
static void ClearBI(BPTR FLock, struct Library *IFFBase)
{
if (FLock) UnLock(FLock);
if (IFFBase) CloseLibrary(IFFBase);
}
/*************************************************************************/
extern void ConvertBinToIff(gvType *gv)
{
register struct Library *IFFBase;
register BPTR FLock;
struct FileInfoBlock *Fib;
LONG FSize;
void *FBuffer;
struct BitMap Bm;
if (!(IFFBase = OpenLibrary(IFFNAME, IFFVERSION)))
PrintMsg(ERR_IFFOPENLIB_SD, IFFNAME, IffVersion);
if (!(FLock = Lock(gv->Options.InputfileName, SHARED_LOCK)))
CLRPRMSGBI((ERR_IFFLOCK_S, gv->Options.InputfileName));
if (!(Fib = (struct FileInfoBlock *)malloc(sizeof (struct FileInfoBlock))))
CLRPRMSGBI((ERR_IFFMEM));
if (!Examine(FLock, Fib))
CLRPRMSGBI((ERR_IFFEXAM_S, gv->Options.InputfileName));
UnLock(FLock);
FLock = NULL;
if (!(FSize = Fib->fib_Size))
CLRPRMSGBI((ERR_IFFSIZE_S, gv->Options.InputfileName));
free(Fib);
if (!(FBuffer = malloc(FSize))) CLRPRMSGBI((ERR_IFFMEM));
if (!(gv->Inputfile = fopen(gv->Options.InputfileName, "rb")))
CLRPRMSGBI((ERR_OPEN_S, gv->Options.InputfileName));
if (FSize != fread(FBuffer, 1, FSize, gv->Inputfile))
CLRPRMSGBI((ERR_READ_S, gv->Options.InputfileName));
Bm.BytesPerRow = (UWORD) (CALCWIDTH(gv->Options.Width, 16) / 8);
Bm.Rows = (UWORD) gv->Options.Height;
Bm.Flags = 0;
Bm.Depth = 1;
Bm.pad = 0;
Bm.Planes[0] = FBuffer;
if (!IFFL_SaveBitMap(gv->Options.OutputfileName, &Bm, NULL, 1))
CLRPRMSGBI((ERR_WRITE_S, gv->Options.OutputfileName));
gv->Outputfile = NULL;
free(FBuffer);
CloseLibrary(IFFBase);
}
/*************************************************************************/
static MsgTypePtr GetErr(int IffErr)
{
u_char i = 0;
static struct { int IffErr;
MsgTypePtr Msg;
} ErrTab[] = { { IFFL_ERROR_OPEN, ERR_OPEN_S },
{ IFFL_ERROR_READ, ERR_READ_S },
{ IFFL_ERROR_NOMEM, ERR_IFFMEM },
{ IFFL_ERROR_NOILBM, ERR_IFFNOILBM_S },
{ IFFL_ERROR_NOBMHD, ERR_IFFNOBMHD_S },
{ IFFL_ERROR_NOBODY, ERR_IFFNOBODY_S },
{ IFFL_ERROR_BADCOMPRESSION,
ERR_IFFBADCOMP_S },
{ 0, ERR_IFFUNKNOWN_S }
};
do {
if (IffErr == ErrTab[i].IffErr) break;
} while (ErrTab[++i].IffErr);
return (ErrTab[i].Msg);
}
/*************************************************************************/
static void ClearIB(IFFL_HANDLE IffFile, struct Library *IFFBase)
{
if (IffFile) IFFL_CloseIFF(IffFile);
if (IFFBase) CloseLibrary(IFFBase);
}
/*************************************************************************/
extern void IffToBin(gvType *gv)
{
register struct Library *IFFBase;
register IFFL_HANDLE IffFile = NULL;
LONG IffErr,
*pBody,
BodySize;
APTR pDest;
struct IFFL_BMHD *BmHd;
if (!(IFFBase = OpenLibrary(IFFNAME, IFFVERSION))) {
PrintMsg(CHANGEATTR(MSGATT_NOERRTXT | MSGATT_RETURN), ERR_IFFOPENLIB_SD,
IFFNAME, IffVersion);
PrintMsg(MSG_ASSUMBIN_S, gv->Options.InputfileName);
return;
}
if (EOF == fclose(gv->Inputfile)) CLRPRMSGIB(ERR_CLOSE_S);
gv->Inputfile = NULL;
if (!(IffFile = IFFL_OpenIFF(gv->Options.InputfileName, IFFL_MODE_READ))) {
if ((IffErr = IFFL_IFFError()) == IFFL_ERROR_NOTIFF) {
CLRPRMSGIB(ERR_IFFNOIFF_S);
if (!(gv->Inputfile = fopen(gv->Options.InputfileName, "rb")))
PrintMsg(ERR_OPEN_S, gv->Options.InputfileName);
return;
}
CLRPRMSGIB(GetErr(IffErr));
}
if(!(BmHd = IFFL_GetBMHD(IffFile))) CLRPRMSGIB(ERR_IFFNOBMHD_S);
if(BmHd->nPlanes != 1) CLRPRMSGIB(ERR_IFFNO1PL_S);
gv->Options.Width = BmHd->w;
gv->Options.Height = BmHd->h;
if (!(pBody = IFFL_FindChunk(IffFile, ID_BODY)))
CLRPRMSGIB(ERR_IFFNOBODY_S);
if (!(BodySize = *++pBody)) CLRPRMSGIB(ERR_IFFSIZE_S);
pBody++; /* now point to data */
gv->Options.InputfileName = TEMPFILENAME;
if (!(gv->Inputfile = fopen(TEMPFILENAME, "wb")))
CLRPRMSGIB(ERR_OPEN_S);
if (BmHd->compression == IFFL_COMPR_NONE) {
if (BodySize != fwrite((void *) pBody, 1, BodySize, gv->Inputfile))
CLRPRMSGIB(ERR_WRITE_S);
}
else {
BodySize = (CALCWIDTH(BmHd->w, 16) / 8) * BmHd->h; /* uncompressed */
if (!(pDest = (APTR) malloc(BodySize))) CLRPRMSGIB(ERR_IFFMEM);
if (BodySize != IFFL_DecompressBlock((APTR) pBody, pDest, BodySize,
BmHd->compression))
CLRPRMSGIB(GetErr(IFFL_IFFError()));
free(pDest);
if (BodySize != fwrite((void *) pDest, 1, BodySize, gv->Inputfile))
CLRPRMSGIB(ERR_WRITE_S);
}
if (EOF == fclose(gv->Inputfile)) CLRPRMSGIB(ERR_CLOSE_S);
if (!(gv->Inputfile = fopen(TEMPFILENAME, "rb"))) CLRPRMSGIB(ERR_OPEN_S);
IFFL_CloseIFF(IffFile);
CloseLibrary(IFFBase);
}
#endif /* AMIGA */